//
//  ViewController.m
//  UI17 - Mail-List
//
//  Created by 韩旭 on 16/8/5.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "MailModel.h"
#import "HX_tableViewCell.h"
#import "HX_DetailViewController.h"
#import "HX_AddMailList.h"
#import "HX_SQLiteManger.h"
#define kTableView @"tableView"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, HX_AddMailListDelegate>
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) NSMutableArray *arrUser;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HX_SQLiteManger *manger;
@end

@implementation ViewController
- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        _arrDataSource = [NSMutableArray array];
        _arrDataSource = [[_manger selectName] mutableCopy];
//        NSString *path = NSUSER;
//        [self.arrDataSource addObjectsFromArray: [NSKeyedUnarchiver unarchiveObjectWithFile:path]];
        if (_arrDataSource.count == 0 ) {
            _arrDataSource = [NSMutableArray array];
            _arrDataSource = [[MailModel groupArray] mutableCopy];
        }
    }
    return _arrDataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"DLS通讯录";
    _manger = [[HX_SQLiteManger alloc] init];
    [_manger openSqlite3];
    [_manger creatTable];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btnLeft.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
    [btnLeft addTarget:self action:@selector(creatViewController) forControlEvents:UIControlEventTouchUpInside];
    [self creatTableView];
}
- (void)creatViewController {
    HX_AddMailList *hx_add = [[HX_AddMailList alloc] init];
    hx_add.delegate = self;
    [self.navigationController pushViewController:hx_add animated:YES];
}
- (void)sandToModle:(MailModel *)model {
    [self.arrDataSource addObject:model];
//    NSString *string = NSUSER;
//    BOOL isPanduan = [NSKeyedArchiver archiveRootObject:_arrDataSource toFile:string];
//    if (isPanduan) {
//        NSLog(@"收藏成功");
//    } else {
//        NSLog(@"收藏失败");
//    }
//    NSLog(@"%@", string);
   [_manger insertModel:model];
    [_tableView reloadData];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"HX_tableViewCell" bundle:nil] forCellReuseIdentifier:kTableView];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HX_tableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
    cell.modelMail = _arrDataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HX_DetailViewController *hx_Detail = [[HX_DetailViewController alloc] init];
    hx_Detail.model = _arrDataSource[indexPath.row];
    
    [self.navigationController pushViewController:hx_Detail animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_arrDataSource removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//     NSString *path = NSUSER;
//    [NSKeyedArchiver archiveRootObject:_arrDataSource toFile:path];
        [_manger deleteAll];
    }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_arrDataSource exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 1;
}
// NSFileManger 的简单使用
// NSFileManger :文件管理类
//使用 degaultManger 类方法，创建出单利对象
//1. 创建对象
- (void)creatFiledManger {
//    创建对象
 NSString *string =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSFileManager *fileManger = [NSFileManager defaultManager];
//    2. 创建文件夹, 将要创建的文件夹拼接到路径上
    NSString *newPatj1 = [string stringByAppendingPathComponent:@"hx1"];
//    路径, 包含要创建的新文件夹 设置是否可以同时创建多级目录下的文件夹
//    
BOOL is = [fileManger createDirectoryAtPath:newPatj1 withIntermediateDirectories:YES attributes:nil error:nil];
    if (is) {
        NSLog(@"文件创建成功");
    } else {
        NSLog(@"文件创建失败");
   }
//    写入文件
    NSString *strData = @"SDFSDFSDFDS";
    BOOL is2 = [strData writeToFile:newPatj1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (is2) {
        NSLog(@"文件写入");
    } else {
        NSLog(@"文件没有写入");
    }
    NSLog(@"%@", newPatj1);
//判断文件是否存在
    if ([fileManger fileExistsAtPath:newPatj1]) {
        NSLog(@"文件存在");
    } else {
        NSLog(@"文件不存在");
    }

    BOOL res3 = [fileManger removeItemAtPath:newPatj1 error:nil];
    if (res3) {
        NSLog(@"文件删除成功");
    } else {
        NSLog(@"文件删除失败");
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
